<template>
  <Sidebar ref="sidebarRef" :title="$t('baseStyle.title')">
    <div
      class="sidebarContent customScrollbar"
      :class="{ isDark: isDark }"
      v-if="data"
    >
      <!-- 背景 -->
      <div class="title noTop">{{ $t('baseStyle.background') }}</div>
      <div class="row">
        <el-tabs class="tab" v-model="activeTab">
          <el-tab-pane :label="$t('baseStyle.color')" name="color">
            <Color
              :color="style.backgroundColor"
              @change="
                color => {
                  update('backgroundColor', color)
                }
              "
            ></Color>
          </el-tab-pane>
          <el-tab-pane :label="$t('baseStyle.image')" name="image">
            <ImgUpload
              class="imgUpload"
              v-model="style.backgroundImage"
              @change="
                img => {
                  update('backgroundImage', img)
                }
              "
            ></ImgUpload>
            <!-- 图片重复方式 -->
            <div class="rowItem">
              <span class="name">{{ $t('baseStyle.imageRepeat') }}</span>
              <el-select
                size="small"
                style="width: 120px"
                v-model="style.backgroundRepeat"
                placeholder=""
                @change="
                  value => {
                    update('backgroundRepeat', value)
                  }
                "
              >
                <el-option
                  v-for="item in backgroundRepeatListShow"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <!-- 图片位置 -->
            <div class="rowItem">
              <span class="name">{{ $t('baseStyle.imagePosition') }}</span>
              <el-select
                size="small"
                style="width: 120px"
                v-model="style.backgroundPosition"
                placeholder=""
                @change="
                  value => {
                    update('backgroundPosition', value)
                  }
                "
              >
                <el-option
                  v-for="item in backgroundPositionListShow"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <!-- 图片大小 -->
            <div class="rowItem">
              <span class="name">{{ $t('baseStyle.imageSize') }}</span>
              <el-select
                size="small"
                style="width: 120px"
                v-model="style.backgroundSize"
                placeholder=""
                @change="
                  value => {
                    update('backgroundSize', value)
                  }
                "
              >
                <el-option
                  v-for="item in backgroundSizeListShow"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
            <!-- 内置背景图片 -->
            <div
              class="rowItem spaceBetween"
              style="margin-top: 8px; margin-bottom: 8px;"
              v-if="bgList.length > 0"
            >
              <div class="name">{{ $t('baseStyle.builtInBackgroundImage') }}</div>
              <div
                class="iconBtn el-icon-arrow-down"
                :class="{ top: !bgListExpand }"
                @click="bgListExpand = !bgListExpand"
              ></div>
            </div>
            <div class="bgList" :class="{ expand: bgListExpand }">
              <div
                class="bgItem"
                v-for="(item, index) in bgList"
                :key="index"
                :class="{active: style.backgroundImage === item}"
                @click="useBg(item)"
              >
                <img :src="item" alt="" />
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <!-- 连线 -->
      <div class="title">{{ $t('baseStyle.line') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.color') }}</span>
          <el-popover
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <span
                class="block"
                :style="{ backgroundColor: style.lineColor }"
              ></span>
            </template>
            <Color
              :color="style.lineColor"
              @change="
                color => {
                  update('lineColor', color)
                }
              "
            ></Color>
          </el-popover>
        </div>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.width') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.lineWidth"
            placeholder=""
            @change="
              value => {
                update('lineWidth', value)
              }
            "
          >
            <el-option
              v-for="item in lineWidthListShow"
              :key="item"
              :label="item"
              :value="item"
            >
              <span
                v-if="item > 0"
                class="borderLine"
                :class="{ isDark: isDark }"
                :style="{ height: item + 'px' }"
              ></span>
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="row">
        <!-- 线宽 -->
        <div class="rowItem" v-if="lineStyleListShow.length > 1">
          <span class="name">{{ $t('baseStyle.style') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.lineStyle"
            placeholder=""
            @change="
              value => {
                update('lineStyle', value)
              }
            "
          >
            <el-option
              v-for="item in lineStyleListShow"
              :key="item.value"
              :label="item.name"
              :value="item.value"
              class="lineStyleOption"
              :class="{
                isDark: isDark,
                active: style.lineStyle === item.value
              }"
            >
              <svg width="120" height="34">
                <line
                  x1="10"
                  y1="17"
                  x2="110"
                  y2="17"
                  stroke-width="2"
                  :stroke="
                    style.lineStyle === item.value
                      ? '#409eff'
                      : isDark
                      ? '#fff'
                      : '#000'
                  "
                  :stroke-dasharray="lineStyleMap[item.value]"
                ></line>
              </svg>
            </el-option>
          </el-select>
        </div>
        <!-- 显示连线标记 -->
        <div class="rowItem" v-if="supportLineStyleLayoutsMap[currentLayout]">
          <span class="name">{{ $t('baseStyle.showLineMarker') }}</span>
          <el-switch
            v-model="style.showLineMarker"
            @change="
              value => {
                update('showLineMarker', value)
              }
            "
          ></el-switch>
        </div>
      </div>
      <div class="row" v-if="supportRootLineKeepSameInCurveLayouts.includes(currentLayout)">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.rootLineKeepSameInCurve') }}</span>
          <el-select
            size="small"
            style="width: 120px"
            v-model="style.rootLineKeepSameInCurve"
            placeholder=""
            @change="
              value => {
                update('rootLineKeepSameInCurve', value)
              }
            "
          >
            <el-option
              v-for="item in rootLineKeepSameInCurveListShow"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="row" v-if="supportRootLineKeepSameInCurveLayouts.includes(currentLayout)">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.rootLineStartPositionKeepSameInCurve') }}</span>
          <el-switch
            v-model="style.rootLineStartPositionKeepSameInCurve"
            @change="
              value => {
                update('rootLineStartPositionKeepSameInCurve', value)
              }
            "
          ></el-switch>
        </div>
      </div>
      <div class="row" v-if="supportLineRadiusLayouts.includes(currentLayout)">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.lineRadius') }}</span>
          <el-slider
            :max="50"
            style="width: 200px"
            v-model="style.lineRadius"
            @change="
              value => {
                update('lineRadius', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <!-- <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.rainbowLines') }}</span>
          <el-switch
            v-model="style.lineFlow"
            @change="
              value => {
                update('lineFlow', value)
              }
            "
          ></el-switch>
        </div>
      </div> -->
      <div class="row" v-if="style.lineFlow">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.lineFlowForward') }}</span>
          <el-switch
            v-model="style.lineFlowForward"
            @change="
              value => {
                update('lineFlowForward', value)
              }
            "
          ></el-switch>
        </div>
      </div>
      <div class="row" v-if="style.lineFlow">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.lineFlowDuration') }}</span>
          <el-slider
            :min="0.1"
            :max="10"
            :step="0.1"
            style="width: 200px"
            v-model="style.lineFlowDuration"
            @change="
              value => {
                update('lineFlowDuration', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <div class="row" v-if="supportNodeUseLineStyleLayouts.includes(currentLayout)">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.nodeUseLineStyle') }}</span>
          <el-switch
            v-model="style.nodeUseLineStyle"
            @change="
              value => {
                update('nodeUseLineStyle', value)
              }
            "
          ></el-switch>
        </div>
      </div>
      <!-- 彩虹线条 -->
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.rainbowLines') }}</span>
          <el-popover
            v-model:visible="rainbowLinesPopoverVisible"
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <div class="curRainbowLine">
                <div
                  v-if="curRainbowLineColorList"
                  style="display: flex; align-items: center; height: 100%;"
                >
                  <div
                    v-for="(color, index) in curRainbowLineColorList"
                    :key="index"
                    :style="{
                      width: '20px',
                      height: '10px',
                      backgroundColor: color
                    }"
                  ></div>
                </div>
                <span v-else>{{ $t('baseStyle.notUseRainbowLines') }}</span>
              </div>
            </template>
            <div class="rainbowLinesBox">
              <div
                class="rainbowLinesItem"
                v-for="(item, index) in rainbowLinesOptions"
                :key="index"
                @click="updateRainbowLinesConfig(item)"
              >
                <div
                  v-if="item.list"
                  style="display: flex; align-items: center; height: 100%;"
                >
                  <div
                    v-for="(color, colorIndex) in item.list"
                    :key="colorIndex"
                    :style="{
                      width: '20px',
                      height: '10px',
                      backgroundColor: color
                    }"
                  ></div>
                </div>
                <span v-else>{{ $t('baseStyle.notUseRainbowLines') }}</span>
              </div>
            </div>
          </el-popover>
        </div>
      </div>
      <!-- 概括 -->
      <div class="title">{{ $t('baseStyle.lineOfOutline') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.color') }}</span>
          <el-popover
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <span
                class="block"
                :style="{ backgroundColor: style.generalizationLineColor }"
              ></span>
            </template>
            <Color
              :color="style.generalizationLineColor"
              @change="
                color => {
                  update('generalizationLineColor', color)
                }
              "
            ></Color>
          </el-popover>
        </div>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.width') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.generalizationLineWidth"
            placeholder=""
            @change="
              value => {
                update('generalizationLineWidth', value)
              }
            "
          >
            <el-option
              v-for="item in lineWidthListShow"
              :key="item"
              :label="item"
              :value="item"
            >
              <span
                v-if="item > 0"
                class="borderLine"
                :class="{ isDark: isDark }"
                :style="{ height: item + 'px' }"
              ></span>
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- 关联�?-->
      <div class="title">{{ $t('baseStyle.associativeLine') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.associativeLineColor') }}</span>
          <el-popover
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <span
                class="block"
                :style="{ backgroundColor: style.associativeLineColor }"
              ></span>
            </template>
            <Color
              :color="style.associativeLineColor"
              @change="
                color => {
                  update('associativeLineColor', color)
                }
              "
            ></Color>
          </el-popover>
        </div>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.associativeLineWidth') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.associativeLineWidth"
            placeholder=""
            @change="
              value => {
                update('associativeLineWidth', value)
              }
            "
          >
            <el-option
              v-for="item in lineWidthListShow"
              :key="item"
              :label="item"
              :value="item"
            >
              <span
                v-if="item > 0"
                class="borderLine"
                :class="{ isDark: isDark }"
                :style="{ height: item + 'px' }"
              ></span>
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{
            $t('baseStyle.associativeLineActiveColor')
          }}</span>
          <el-popover
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <span
                class="block"
                :style="{ backgroundColor: style.associativeLineActiveColor }"
              ></span>
            </template>
            <Color
              :color="style.associativeLineActiveColor"
              @change="
                color => {
                  update('associativeLineActiveColor', color)
                }
              "
            ></Color>
          </el-popover>
        </div>
        <div class="rowItem">
          <span class="name">{{
            $t('baseStyle.associativeLineActiveWidth')
          }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.associativeLineActiveWidth"
            placeholder=""
            @change="
              value => {
                update('associativeLineActiveWidth', value)
              }
            "
          >
            <el-option
              v-for="item in lineWidthListShow"
              :key="item"
              :label="item"
              :value="item"
            >
              <span
                v-if="item > 0"
                class="borderLine"
                :class="{ isDark: isDark }"
                :style="{ height: item + 'px' }"
              ></span>
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('style.style') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.associativeLineDasharray"
            placeholder=""
            @change="
              value => {
                update('associativeLineDasharray', value)
              }
            "
          >
            <el-option
              v-for="item in borderDasharrayListShow"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
              <svg width="120" height="34">
                <line
                  x1="10"
                  y1="17"
                  x2="110"
                  y2="17"
                  stroke-width="2"
                  :stroke="
                    style.associativeLineDasharray === item.value
                      ? '#409eff'
                      : isDark
                      ? '#fff'
                      : '#000'
                  "
                  :stroke-dasharray="item.value"
                ></line>
              </svg>
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- 关联线文字-->
      <div class="title">{{ $t('baseStyle.associativeLineText') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.fontFamily') }}</span>
          <el-select
            size="small"
            v-model="style.associativeLineTextFontFamily"
            placeholder=""
            @change="update('associativeLineTextFontFamily', $event)"
          >
            <el-option
              v-for="item in fontFamilyListShow"
              :key="item.value"
              :label="item.name"
              :value="item.value"
              :style="{ fontFamily: item.value }"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.color') }}</span>
          <el-popover
            placement="bottom"
            trigger="click"
            width="auto"
          >
            <template #reference>
              <span
                class="block"
                :style="{ backgroundColor: style.associativeLineTextColor }"
              ></span>
            </template>
            <Color
              :color="style.associativeLineTextColor"
              @change="
                color => {
                  update('associativeLineTextColor', color)
                }
              "
            ></Color>
          </el-popover>
        </div>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.fontSize') }}</span>
          <el-select
            size="small"
            style="width: 80px"
            v-model="style.associativeLineTextFontSize"
            placeholder=""
            @change="update('associativeLineTextFontSize', $event)"
          >
            <el-option
              v-for="item in fontSizeListShow"
              :key="item"
              :label="item"
              :value="item"
              :style="{ fontSize: item + 'px' }"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- 节点内边�?-->
      <div class="title">{{ $t('baseStyle.nodePadding') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.horizontal') }}</span>
          <el-slider
            :max="50"
            style="width: 200px"
            v-model="style.paddingX"
            @change="
              value => {
                update('paddingX', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.vertical') }}</span>
          <el-slider
            :max="50"
            style="width: 200px"
            v-model="style.paddingY"
            @change="
              value => {
                update('paddingY', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <!-- 图片最大宽�?-->
      <!-- <div class="title">{{ $t('baseStyle.imgMaxSize') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.width') }}</span>
          <el-slider
            :max="1000"
            style="width: 200px"
            v-model="style.imgMaxWidth"
            @change="
              value => {
                update('imgMaxWidth', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.height') }}</span>
          <el-slider
            :max="1000"
            style="width: 200px"
            v-model="style.imgMaxHeight"
            @change="
              value => {
                update('imgMaxHeight', value)
              }
            "
          ></el-slider>
        </div>
      </div> -->
      <!-- 图标大小 -->
      <div class="title">{{ $t('baseStyle.icon') }}</div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.size') }}</span>
          <el-slider
            :max="100"
            style="width: 200px"
            v-model="style.iconSize"
            @change="
              value => {
                update('iconSize', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <!-- 二级节点外边�?-->
      <div class="title">{{ $t('baseStyle.nodeMargin') }}</div>
      <div class="row column noBottom">
        <el-tabs
          class="tab"
          v-model="marginActiveTab"
          @tab-click="initMarginStyle"
        >
          <el-tab-pane
            :label="$t('baseStyle.level2Node')"
            name="second"
          ></el-tab-pane>
          <el-tab-pane
            :label="$t('baseStyle.belowLevel2Node')"
            name="node"
          ></el-tab-pane>
        </el-tabs>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.horizontal') }}</span>
          <el-slider
            :max="200"
            style="width: 200px"
            v-model="style.marginX"
            @change="
              value => {
                updateMargin('marginX', value)
              }
            "
          ></el-slider>
        </div>
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.vertical') }}</span>
          <el-slider
            :max="200"
            style="width: 200px"
            v-model="style.marginY"
            @change="
              value => {
                updateMargin('marginY', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <!-- 外框内边�?-->
      <div class="title">{{ $t('baseStyle.outerFramePadding') }}</div>
      <div class="row noBottom">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.horizontal') }}</span>
          <el-slider
            style="width: 200px"
            v-model="outerFramePadding.outerFramePaddingX"
            @change="
              value => {
                updateOuterFramePadding('outerFramePaddingX', value)
              }
            "
          ></el-slider>
        </div>
      </div>
      <div class="row">
        <div class="rowItem">
          <span class="name">{{ $t('baseStyle.vertical') }}</span>
          <el-slider
            style="width: 200px"
            v-model="outerFramePadding.outerFramePaddingY"
            @change="
              value => {
                updateOuterFramePadding('outerFramePaddingY', value)
              }
            "
          ></el-slider>
        </div>
      </div>
    </div>
  </Sidebar>
</template>

<script setup>
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue'
import { useI18n } from 'vue-i18n'
import { storeToRefs } from 'pinia'
import { useMainStore } from '@/stores'
import { useEventBus } from '@/hooks'
import Sidebar from './Sidebar.vue'
import Color from './Color.vue'
import {
  lineWidthList,
  lineStyleList,
  backgroundRepeatList,
  backgroundPositionList,
  backgroundSizeList,
  fontFamilyList,
  fontSizeList,
  rootLineKeepSameInCurveList,
  lineStyleMap,
  borderDasharrayList
} from '@/config'
import ImgUpload from '@/components/ImgUpload/index.vue'
import { storeData, storeConfig } from '@/api'
import {
  supportLineStyleLayoutsMap,
  supportLineRadiusLayouts,
  supportNodeUseLineStyleLayouts,
  supportRootLineKeepSameInCurveLayouts,
  rainbowLinesOptions
} from '@/config/constant'

// 定义props
const props = defineProps({
  data: {
    type: [Object, null]
  },
  configData: {
    type: Object
  },
  mindMap: {
    type: Object
  }
})

// 组合式API设置
const { t, locale } = useI18n()
const { $on, $off, $emit, $once } = useEventBus()
const store = useMainStore()
const { activeSidebar, localConfig, isDark, bgList } = storeToRefs(store)

// 响应式数�
const activeTab = ref('color')
const marginActiveTab = ref('second')
const style = ref({
  backgroundColor: '',
  lineColor: '',
  lineWidth: '',
  lineStyle: '',
  showLineMarker: '',
  rootLineKeepSameInCurve: '',
  rootLineStartPositionKeepSameInCurve: '',
  lineRadius: 0,
  lineFlow: false,
  lineFlowForward: true,
  lineFlowDuration: 1,
  generalizationLineWidth: '',
  generalizationLineColor: '',
  associativeLineColor: '',
  associativeLineWidth: 0,
  associativeLineActiveWidth: 0,
  associativeLineDasharray: '',
  associativeLineActiveColor: '',
  associativeLineTextFontSize: 0,
  associativeLineTextColor: '',
  associativeLineTextFontFamily: '',
  paddingX: 0,
  paddingY: 0,
  imgMaxWidth: 0,
  imgMaxHeight: 0,
  iconSize: 0,
  backgroundImage: '',
  backgroundRepeat: 'no-repeat',
  backgroundPosition: '',
  backgroundSize: '',
  marginX: 0,
  marginY: 0,
  nodeUseLineStyle: false
})
const rainbowLinesPopoverVisible = ref(false)
const curRainbowLineColorList = ref(null)
const currentLayout = ref('')
const outerFramePadding = ref({
  outerFramePaddingX: 0,
  outerFramePaddingY: 0
})
const bgListExpand = ref(true)

// 模板引用
const sidebarRef = ref(null)

// 计算属�?
const lineStyleListShow = computed(() => {
  return lineStyleList[locale.value] || lineStyleList.zh
})

const rootLineKeepSameInCurveListShow = computed(() => {
  return rootLineKeepSameInCurveList[locale.value] || rootLineKeepSameInCurveList.zh
})

const backgroundRepeatListShow = computed(() => {
  return backgroundRepeatList[locale.value] || backgroundRepeatList.zh
})

const backgroundPositionListShow = computed(() => {
  return backgroundPositionList[locale.value] || backgroundPositionList.zh
})

const fontSizeListShow = computed(()=>{
  return fontSizeList[locale.value] || fontSizeList.zh
})

const lineWidthListShow = computed(()=>{
  return lineWidthList[locale.value] || lineWidthList.zh
})

const borderDasharrayListShow = computed(()=>{
  return borderDasharrayList[locale.value] || borderDasharrayList.zh
})

const fontFamilyListShow = computed(()=>{
  return fontFamilyList[locale.value] || fontFamilyList.zh
})

const backgroundSizeListShow = computed(() => {
  return backgroundSizeList[locale.value] || backgroundSizeList.zh
})

// 监听�?
watch(activeSidebar, (val) => {
  if (val === 'baseStyle') {
    sidebarRef.value.show = true
    initStyle()
    initOuterFramePadding()
    initMarginStyle()
    initRainbowLinesConfig()
  } else {
    sidebarRef.value.show = false
  }
})

// 生命周期钩子
onMounted(() => {
  if (props.mindMap) {
    currentLayout.value = props.mindMap.getLayout()
    props.mindMap.on('layout_change', handleLayoutChange)
  }
})

onBeforeUnmount(() => {
  if (props.mindMap) {
    props.mindMap.off('layout_change', handleLayoutChange)
  }
})

// 方法
const handleLayoutChange = (layout) => {
  currentLayout.value = layout
}

const initRainbowLinesConfig = () => {
  if (props.configData && props.configData.rainbowLinesConfig) {
    curRainbowLineColorList.value = props.configData.rainbowLinesConfig.open
      ? props.configData.rainbowLinesConfig.colorsList
      : null
  }
}

const initStyle = () => {
  const themeConfig = props.mindMap.getThemeConfig()
  Object.keys(style.value).forEach(key => {
    if (key === 'marginX' || key === 'marginY') return
    style.value[key] = themeConfig[key]
  })
}

const initOuterFramePadding = () => {
  outerFramePadding.value.outerFramePaddingX = props.mindMap.getConfig('outerFramePaddingX')
  outerFramePadding.value.outerFramePaddingY = props.mindMap.getConfig('outerFramePaddingY')
}

const initMarginStyle = () => {
  ;['marginX', 'marginY'].forEach(key => {
    style.value[key] = props.mindMap.getThemeConfig()[marginActiveTab.value][key]
  })
}

const update = (key, value) => {
  if (key === 'backgroundImage' && value === 'none') {
    style.value[key] = ''
  } else {
    style.value[key] = value
  }
  props.data.theme.config[key] = value
  $emit('showLoading')
  props.mindMap.setThemeConfig(props.data.theme.config)
  storeData({
    theme: {
      template: props.mindMap.getTheme(),
      config: props.data.theme.config
    }
  })
}

const updateRainbowLinesConfig = (item) => {
  rainbowLinesPopoverVisible.value = false
  curRainbowLineColorList.value = item.list || null
  let newConfig = null
  if (item.list) {
    newConfig = {
      open: true,
      colorsList: item.list
    }
  } else {
    newConfig = {
      open: false
    }
  }
  props.configData.rainbowLinesConfig = newConfig
  props.mindMap.rainbowLines.updateRainLinesConfig(newConfig)
  storeConfig(props.configData)
}

const updateOuterFramePadding = (prop, value) => {
  outerFramePadding.value[prop] = value
  props.configData[prop] = value
  props.mindMap.updateConfig({
    [prop]: value
  })
  storeConfig(props.configData)
  props.mindMap.render()
}

const updateMargin = (type, value) => {
  style.value[type] = value
  if (!props.data.theme.config[marginActiveTab.value]) {
    props.data.theme.config[marginActiveTab.value] = {}
  }
  props.data.theme.config[marginActiveTab.value][type] = value
  props.mindMap.setThemeConfig(props.data.theme.config)
  storeData({
    theme: {
      template: props.mindMap.getTheme(),
      config: props.data.theme.config
    }
  })
}

const useBg = (bg) => {
  update('backgroundImage', bg)
}
</script>

<style lang="less" scoped>
.sidebarContent {
  padding: 20px;
  padding-top: 10px;

  &.isDark {
    .title {
      color: #fff;
    }

    .row {
      .rowItem {
        .name,
        .curRainbowLine {
          color: hsla(0, 0%, 100%, 0.6);
        }
      }
    }
  }

  .title {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(26, 26, 26, 0.9);
    margin-bottom: 10px;
    margin-top: 35px;

    &.noTop {
      margin-top: 0;
    }
  }

  .row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;

    &.noBottom {
      margin-bottom: 0;
    }

    &.column {
      flex-direction: column;
    }

    .tab {
      width: 100%;
    }

    .imgUpload {
      margin-bottom: 5px;
    }

    .btnGroup {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

    .rowItem {
      display: flex;
      align-items: center;
      margin-bottom: 5px;

      &.spaceBetween {
        justify-content: space-between;
      }

      .name {
        font-size: 12px;
        margin-right: 10px;
        white-space: nowrap;
      }

      .block {
        display: inline-block;
        width: 30px;
        height: 30px;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        cursor: pointer;
      }

      .curRainbowLine {
        height: 24px;
        border: 1px solid #dcdfe6;
        font-size: 12px;
        width: 240px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }

      .iconBtn {
        cursor: pointer;
        transition: all 0.3s;

        &.top {
          transform: rotateZ(-180deg);
        }
      }
    }

    .styleBtn {
      position: relative;
      width: 50px;
      height: 30px;
      background: #fff;
      border: 1px solid #eee;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      cursor: pointer;
      border-radius: 4px;

      &.actived {
        background-color: #eee;
      }

      .colorShow {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        height: 2px;
      }
    }

    .bgList {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      height: 75px;

      &.expand {
        height: max-content;
      }

      .bgItem {
        width: 120px;
        height: 73px;
        border: 1px solid #e9e9e9;
        border-radius: 5px;
        overflow: hidden;
        padding: 5px;
        margin-bottom: 8px;
        cursor: pointer;

        &.active {
          border-color: #409eff;
        }

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
  }
}

.borderLine {
  display: inline-block;
  width: 100%;
  background-color: #000;

  &.isDark {
    background-color: #fff;
  }
}
</style>
<style lang="less">
.el-select-dropdown__item.selected {
  .borderLine {
    background-color: #409eff;
  }
}

.lineStyleOption {
  &.isDark {
    svg {
      path {
        stroke: #fff;
      }
    }
  }

  &.isSelected {
    svg {
      path {
        stroke: #409eff;
      }
    }
  }

  svg {
    margin-top: 4px;

    path {
      stroke: #000;
    }
  }
}

.rainbowLinesOptionsBox {
  width: 200px;

  &.isDark {
    .optionItem {
      color: hsla(0, 0%, 100%, 0.6);

      &:hover {
        background-color: hsla(0, 0%, 100%, 0.05);
      }
    }
  }

  .optionItem {
    width: 100%;
    height: 30px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;

    &:hover {
      background-color: #f5f7fa;
    }
  }
}

.colorsBar {
  display: flex;
  width: 100%;
  height: 100%;
  align-items: center;

  .colorItem {
    flex: 1;
    height: 15px;
  }
}
</style>
